package com.xuhf.consumerPullPush;

import com.xuhf.config.Config;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;

public class Produce01 {

    public static void main(String[] args) throws Exception {
        //1 创建一个生产者对象, 并且指定一个生产者组
        DefaultMQProducer producer = new DefaultMQProducer(Config.PRODUCER_GROUP);
        //2 设置名字服务的地址
        producer.setNamesrvAddr(Config.NAMESRV_ADDR);
        //3 启动生产者
        producer.start();

        for (int i = 0; i < 1000; i++) {
            //4 创建一个消息
            Message message = new Message("03-pull-push", "tagA", ("pull and push msg " + String.format("%05d", i)).getBytes("utf-8"));
            //5 发送消息
            SendResult sendResult = producer.send(message);
            System.out.println("生产者发送消息：" + sendResult.getMsgId() + "，发送状态：" + sendResult.getSendStatus());
            Thread.sleep(500);
        }
        //6 关闭连接
        producer.shutdown();
    }
}
